package edu.neu.highway.utils;

import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
import edu.neu.highway.vo.flux.FluxQueryParams;
import edu.neu.highway.vo.flux.FluxQueryResult;
import edu.neu.highway.vo.flux.ResultItem;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;

import static edu.neu.highway.constant.Constant.*;

@Slf4j
public class FluxUtils {
    public static String buildFluxQueryString(FluxQueryParams params) {
        StringBuilder strBuilder = new StringBuilder();

        // 1, 指定 Bucket Name
        strBuilder.append(String.format(FLUX_BUCKET_FORMAT, INFLUXDB_BUCKET_NAME));

        // 2, 指定查询时间范围
        strBuilder.append(String.format(RANGE_FORMAT, params.getStart(), params.getStop()));

        // 3, 指定测量标准
        strBuilder.append(String.format(FILTER_MEASUREMENT_FORMAT, params.getMeasurement()));

        // 4, 指定门架/收费站 id
        if (params.getGateId().size() == 1) {
            strBuilder.append(String.format(FILTER_GATE_ID_FORMAT, params.getGateId().get(0)));
        } else {
            strBuilder.append(FILTER_GATE_ID_HEAD);
            for (int i = 0; i < params.getGateId().size() - 1; i++) {
                strBuilder.append(String.format(FILTER_GATE_ID_ITEM_FORMAT, params.getGateId().get(i)));
                strBuilder.append(" or ");
            }
            int l = params.getGateId().size() - 1;
            if(l > 0) {
                strBuilder.append(String.format(FILTER_GATE_ID_ITEM_FORMAT, params.getGateId().get(l)));
                strBuilder.append(")\n");
            }
        }

        // 5, 指定入站口 id
        if(params.getInStation() != null && !"".equals(params.getInStation())) {
            strBuilder.append(String.format(FILTER_IN_STATION_FORMAT, params.getInStation()));
        }

        // 6, 指定车型 id
        if(params.getVehicleType() != null && !"".equals(params.getVehicleType())) {
            strBuilder.append(String.format(FILTER_VEHICLE_TYPE_FORMAT, params.getVehicleType()));
        }

        // 7, 指定目标字段
        strBuilder.append(String.format(FILTER_FIELD_FORMAT, params.getTargetField()));

        // 8, 指定窗口长度与聚集函数
        strBuilder.append(String.format(FLUX_AGGREGATE_FUNCTION_FORMAT, params.getWindowSize(), params.getAggregateFunction()));

        return strBuilder.toString();
    }

    public static FluxQueryResult buildFluxQueryResult(List<FluxTable> tables, FluxQueryParams params) {
        FluxQueryResult fluxQueryResult = new FluxQueryResult();
        fluxQueryResult.setStart(params.getStart());
        fluxQueryResult.setStop(params.getStop());
        Integer size = 0;
        Integer groupNum = 0;

        for (FluxTable table : tables) {
            groupNum ++;
            for (FluxRecord record : table.getRecords()) {
                ResultItem resultItem = new ResultItem();

                if(record.getValue() != null) {
                    resultItem.setData(record.getValue());
                } else {
                    resultItem.setData(0);
                }

                resultItem.setTime(Objects.requireNonNull(record.getTime()).toEpochMilli());
                resultItem.setGateId((String) record.getValues().get("gate_id"));
                resultItem.setField(record.getField());
                resultItem.setInStation((String) record.getValues().get("in_station"));
                resultItem.setVehicleType((String) record.getValues().get("vehicle_type"));

                fluxQueryResult.getResultList().add(resultItem);
                size ++;
            }
        }

        fluxQueryResult.setSize(size);
        fluxQueryResult.setGroupNum(groupNum);
        return fluxQueryResult;
    }
}
